package com.ruoyi.web.controller.liangshi;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.liangshi.domain.BiBsKscDay;
import com.ruoyi.liangshi.domain.GdArchShift;
import com.ruoyi.liangshi.service.IBiBsKscDayService;
import com.ruoyi.liangshi.service.IGdArchShiftService;
import com.ruoyi.liangshi.vo.query.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author LiuJin
 * @Date: Create in 2023-11-30 11:25
 * @Description: this class is
 */
@RequestMapping("/linagshi/bs-ksc-day")
@RestController
public class BiBsKscDayController extends BaseController {
    @Autowired
    private IBiBsKscDayService biBsKscDayService;

    @Autowired
    private IGdArchShiftService gdArchShiftService;

    /**
     * 按日获取数据列表
     * @param type
     * @param date
     * @param shopId
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/getBsKscHourList")
    public TableDataInfo getBsKscHourList( @RequestParam(value = "type") String type,
                                           @RequestParam(value = "date",required = false) String date,
                                           @RequestParam(value = "shopId",required = false) Long shopId,
                                          @RequestParam(value = "beginTime",required = false)String beginTime,
                                          @RequestParam(value = "endTime",required = false)String endTime) {
        startPage();
        if(type.equals("1") && StrUtil.isBlank(beginTime) && StrUtil.isBlank(endTime)){
            endTime=DateUtil.format(new Date(),"yyyy-MM-dd");
            beginTime=DateUtil.format(DateUtil.offsetDay(new Date(),-7),"yyyy-MM-dd");
        }else if (type.equals("2") && StrUtil.isBlank(date)){
            date=DateUtil.format(new Date(),"yyyy-MM");
        }
        List<BiBsKscDayDataVo> biBsKscDayDataList = biBsKscDayService.getBiBsKscDayDataList(type,date,shopId,beginTime,endTime);
        Map<String, GdArchShift> shiftMap = gdArchShiftService.list().stream().collect(Collectors.toMap(item -> item.getId().toString(), obj -> obj));
        for (BiBsKscDayDataVo biBsKscDayDataVo : biBsKscDayDataList) {
            Map<String, BigDecimal> itemTypeMaps = new HashMap<>();
            Map<String, BigDecimal> timeOutItemTypeMaps = new HashMap<>();
            List<BiBsKscDay> kscDayList = biBsKscDayService.lambdaQuery()
                    .eq(BiBsKscDay::getBsDate, biBsKscDayDataVo.getDate())
                    .eq(BiBsKscDay::getBsDay, biBsKscDayDataVo.getDay())
                    .eq(BiBsKscDay::getShift, biBsKscDayDataVo.getShift())
                    .list();
            GdArchShift gdArchShift = shiftMap.get(biBsKscDayDataVo.getShift());
            biBsKscDayDataVo.setShiftName(gdArchShift.getName());
            //筛选菜品类型，然后对菜品数量进行类型
            for (BiBsKscDay biBsKscDay : kscDayList) {
                switch (biBsKscDay.getItemType()) {
                    case "A":
                        BigDecimal itemAcount = itemTypeMaps.get("A");
                        if(ObjectUtil.isNull(itemAcount)){
                            itemAcount=BigDecimal.ZERO;
                        }
                        itemAcount=itemAcount.add(biBsKscDay.getDishesCount());
                        itemTypeMaps.put("A",itemAcount);
                        if(biBsKscDay.getTimeOutType().equals("2")){
                            BigDecimal timeOutItemAcount = timeOutItemTypeMaps.get("A");
                            if(ObjectUtil.isNull(timeOutItemAcount)){
                                timeOutItemAcount=BigDecimal.ZERO;
                            }
                            timeOutItemAcount=timeOutItemAcount.add(biBsKscDay.getDishesCount());
                            timeOutItemTypeMaps.put("A",timeOutItemAcount);
                        }
                        break;
                    case "B":
                        BigDecimal itemBcount = itemTypeMaps.get("B");
                        if(ObjectUtil.isNull(itemBcount)){
                            itemBcount=BigDecimal.ZERO;
                        }
                        itemBcount=itemBcount.add(biBsKscDay.getDishesCount());
                        itemTypeMaps.put("B",itemBcount);
                        if(biBsKscDay.getTimeOutType().equals("2")){
                            BigDecimal timeOutItemBcount = timeOutItemTypeMaps.get("B");
                            if(ObjectUtil.isNull(timeOutItemBcount)){
                                timeOutItemBcount=BigDecimal.ZERO;
                            }
                            timeOutItemBcount=timeOutItemBcount.add(biBsKscDay.getDishesCount());
                            timeOutItemTypeMaps.put("B",timeOutItemBcount);
                        }
                        break;
                    case "C":
                        BigDecimal itemCcount = itemTypeMaps.get("C");
                        if(ObjectUtil.isNull(itemCcount)){
                            itemCcount=BigDecimal.ZERO;
                        }
                        itemCcount=itemCcount.add(biBsKscDay.getDishesCount());
                        itemTypeMaps.put("C",itemCcount);
                        if(biBsKscDay.getTimeOutType().equals("2")){
                            BigDecimal timeOutItemCcount = timeOutItemTypeMaps.get("C");
                            if(ObjectUtil.isNull(timeOutItemCcount)){
                                timeOutItemCcount=BigDecimal.ZERO;
                            }
                            timeOutItemCcount=timeOutItemCcount.add(biBsKscDay.getDishesCount());
                            timeOutItemTypeMaps.put("C",timeOutItemCcount);
                        }
                        break;
                    default:
                        break;

                }
            }
            biBsKscDayDataVo.setItemTypeMaps(itemTypeMaps);
            biBsKscDayDataVo.setTimeOutItemTypeMaps(timeOutItemTypeMaps);
        }
        return getDataTable(biBsKscDayDataList);
    }

    /**
     * 按日获取出餐数量折线图数据
     * @param type
     * @param date
     * @param shopId
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/buildDayDataChart")
    public AjaxResult buildDayDataChart( @RequestParam(value = "type") String type,
                                         @RequestParam(value = "date",required = false) String date,
                                        @RequestParam(value = "shopId",required = false) Long shopId,
                                        @RequestParam(value = "beginTime",required = false)String beginTime,
                                        @RequestParam(value = "endTime",required = false)String endTime) {
        if(type.equals("1") && StrUtil.isBlank(beginTime) && StrUtil.isBlank(endTime)){
            endTime=DateUtil.format(new Date(),"yyyy-MM-dd");
            beginTime=DateUtil.format(DateUtil.offsetDay(new Date(),-7),"yyyy-MM-dd");
        }else if (type.equals("2") && StrUtil.isBlank(date)){
            date=DateUtil.format(new Date(),"yyyy-MM");
        }
        List<BiBsKscDayChartData> bsKscDayChartDataList = biBsKscDayService.getBsKscDayChartDataList(type,date,shopId,beginTime,endTime);
        List<BigDecimal> aDishesCount = bsKscDayChartDataList.stream().map(BiBsKscDayChartData::getADishesTotalCount).collect(Collectors.toList());
        List<BigDecimal> bDishesCount = bsKscDayChartDataList.stream().map(BiBsKscDayChartData::getBDishesTotalCount).collect(Collectors.toList());
        List<BigDecimal> cDishesCount = bsKscDayChartDataList.stream().map(BiBsKscDayChartData::getCDishesTotalCount).collect(Collectors.toList());
        List<BigDecimal> dishesTotalCount = bsKscDayChartDataList.stream().map(BiBsKscDayChartData::getDishesTotalCount).collect(Collectors.toList());
        List<BigDecimal> timeOutDishesCount = bsKscDayChartDataList.stream().map(BiBsKscDayChartData::getTimeOutDishesTotalCount).collect(Collectors.toList());
        List<String> dayData = bsKscDayChartDataList.stream().map(BiBsKscDayChartData::getDay).collect(Collectors.toList());
        AjaxResult result = success();
        result.put("aDishesCount", aDishesCount);
        result.put("bDishesCount", bDishesCount);
        result.put("cDishesCount", cDishesCount);
        result.put("dishesTotalCount", dishesTotalCount);
        result.put("timeOutDishesCount", timeOutDishesCount);
        result.put("dayData", dayData);
        return result;
    }

    /**
     * 按日获取出餐耗时折线图数据
     * @param type
     * @param date
     * @param shopId
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/buildDayAvgTimeDataChart")
    public AjaxResult buildDayAvgTimeDataChart(
                                                @RequestParam(value = "type") String type,
                                                @RequestParam(value = "date",required = false) String date,
                                               @RequestParam(value = "shopId",required = false) Long shopId,
                                               @RequestParam(value = "beginTime",required = false)String beginTime,
                                               @RequestParam(value = "endTime",required = false)String endTime) {
        if(type.equals("1") && StrUtil.isBlank(beginTime) && StrUtil.isBlank(endTime)){
            endTime=DateUtil.format(new Date(),"yyyy-MM-dd");
            beginTime=DateUtil.format(DateUtil.offsetDay(new Date(),-7),"yyyy-MM-dd");
        }else if (type.equals("2") && StrUtil.isBlank(date)){
            date=DateUtil.format(new Date(),"yyyy-MM");
        }
        List<BiBsKscDayAvgTimeChartData> bsKscDayAvgTimeChartDataList = biBsKscDayService.getBsKscDayAvgTimeChartDataList(type,date,shopId,beginTime,endTime);
        List<String> dayData = bsKscDayAvgTimeChartDataList.stream().map(BiBsKscDayAvgTimeChartData::getDay).collect(Collectors.toList());
        List<BigDecimal> avgTotalTime = bsKscDayAvgTimeChartDataList.stream().map(BiBsKscDayAvgTimeChartData::getAvgTotalTime).collect(Collectors.toList());
        List<BigDecimal> avgATotalTime = bsKscDayAvgTimeChartDataList.stream().map(BiBsKscDayAvgTimeChartData::getAvgADishesTime).collect(Collectors.toList());
        List<BigDecimal> avgBTotalTime = bsKscDayAvgTimeChartDataList.stream().map(BiBsKscDayAvgTimeChartData::getAvgBDishesTime).collect(Collectors.toList());
        List<BigDecimal> avgCTotalTime = bsKscDayAvgTimeChartDataList.stream().map(BiBsKscDayAvgTimeChartData::getAvgCDishesTime).collect(Collectors.toList());
        AjaxResult result = success();
        result.put("dayData", dayData);
        result.put("avgTotalTime", avgTotalTime);
        result.put("avgATotalTime", avgATotalTime);
        result.put("avgBTotalTime", avgBTotalTime);
        result.put("avgCTotalTime", avgCTotalTime);
        return result;
    }
}
